#!/bin/sh

local enableUCI="$(uci -q get nginx.global.enableUCI)"

if [ "$enableUCI" != "0" ]; then
	if nginx -V 2>&1 | grep -q ubus && [ -f /usr/lib/nginx/modules/ngx_http_ubus_module.so ]; then
		if [ -z "$(cat /etc/nginx/conf.d/luci.locations | grep ubus)" ]; then
			cat <<EOT >> /etc/nginx/conf.d/luci.locations

location /ubus {
        ubus_interpreter;
        ubus_socket_path /var/run/ubus/ubus.sock;
        ubus_parallel_req 2;
}
EOT
		fi

        	# Drop old reference of luci.module as we migrated to a more automatic approach
        	[ -f "/etc/nginx/module.d/luci.module" ] && rm /etc/nginx/module.d/luci.module
        fi
        grep -q /var/run/ubus.sock /etc/nginx/conf.d/luci.locations &&
	sed -i 's#/var/run/ubus.sock#/var/run/ubus/ubus.sock#' /etc/nginx/conf.d/luci.locations
fi

/etc/init.d/nginx enable
if [ -n "$(pgrep nginx)" ]; then
        /etc/init.d/nginx restart
else
        /etc/init.d/nginx start
fi

/etc/init.d/uwsgi enable
if [ -n "$(pgrep uwsgi)" ]; then
        /etc/init.d/uwsgi restart
else
        /etc/init.d/uwsgi start
fi


exit 0
